package com.pingan.cashier.web;

import com.pingan.cashier.service.PayChannelService;
import com.pingan.cashier.web.req.OrderCreateReq;
import com.pingan.cashier.web.req.PaymentResultCallbackReq;
import com.pingan.cashier.web.resp.ResponseObject;
import com.pingan.cashier.web.vo.PayChannelVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@Slf4j
@RequestMapping("/cashier")
@RestController
public class PayChannelController {

    @Autowired
    private PayChannelService payChannelService;

    /**
     * 获得支付通道列表
     * @return
     */
    @GetMapping("/getPayChannelList")
    @ResponseBody
    public ResponseObject getPayChannelList(){

        ResponseObject responseObject = new ResponseObject();
        responseObject.setCode("200");
        responseObject.setMessage("成功");
        try {
            List<PayChannelVO> payChannelList = payChannelService.queryUserPayChannelList();
            responseObject.setData(payChannelList);
        } catch (Exception e) {
            log.error("获取支付列表失败", e);
            responseObject.setCode("500");
            responseObject.setMessage("失败");
        }
        return responseObject;

    }

    @PostMapping("/createPaymentOrder")
    @ResponseBody
    public ResponseObject createOrder(@RequestBody OrderCreateReq orderCreateReq){

        ResponseObject responseObject = new ResponseObject();
        responseObject.setCode("200");
        responseObject.setMessage("成功");
        try {
            payChannelService.createOrder(orderCreateReq);
        } catch (Exception e) {
            log.error("创建收银单失败", e);
            responseObject.setCode("500");
            responseObject.setMessage("失败");
        }
        return responseObject;

    }

    @PostMapping("/paymentResultCallback")
    @ResponseBody
    public ResponseObject paymentResultCallback(@RequestBody PaymentResultCallbackReq paymentResultCallbackReq){
        ResponseObject responseObject = new ResponseObject();
        responseObject.setCode("200");
        responseObject.setMessage("成功");
        try {
            payChannelService.paymentResultCallback(paymentResultCallbackReq.getPaymentOrder());
        } catch (Exception e) {
            log.error("交易订单结果回调失败", e);
            responseObject.setCode("500");
            responseObject.setMessage("失败");
        }
        return responseObject;
    }
}








